package com.lagou.controller;

import com.lagou.pojo.Resume;
import com.lagou.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author czq
 * @date 2020/7/15 21:52
 */
@Controller
@RequestMapping("resume")
public class ResumeController {

    @Autowired
    private IResumeService resumeService;

    /**
     * 查询用户列表
     * @param model
     * @return
     */
    @RequestMapping("findAll")
    public String findAll(Model model) {
        List<Resume> resumeList = resumeService.findAll();
        model.addAttribute("list", resumeList);
        return "list";
    }

    /**
     * 跳转到 新增用户/编辑用户 页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail")
    public String detail(Long id, Model model) {
        if (id != null) {
            // 如果有传 id，则取数据库查询用户详情返回给页面
            Resume resume = resumeService.findById(id);
            model.addAttribute("resume", resume);
        }
        return "detail";
    }

    /**
     * 新增/编辑 用户
     * @param resume
     * @param model
     * @return
     */
    @RequestMapping("addAndUpdate")
    public String addAndUpdate(Resume resume, Model model) {
        resumeService.addAndUpdate(resume);
        return findAll(model);
    }

    /**
     * 删除用户
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("deleteUser")
    public String deleteUser(Long id, Model model) {
        resumeService.deleteUser(id);
        return findAll(model);
    }
}
